STAT 331

Grades

Dates

Come in all varieties

  • 20150307

  • 2018-03-24 11:07:40

  • 24-03-2018

With functions for each!

  • dmy()

  • ymd()

  • mdy()

  • ymd_h()

  • ymd_hm()

  • ymd_hms()

What would you do?



How would you coerce 20150307 into a datetime object?

Lots of Choices

date <- "20150307"



Simple

ymd(date) 
[1] "2015-03-07"

Complicated

year <- str_sub(date, start = 1, end = 4)
month <- str_sub(date, start = 5, end = 6)
day <- str_sub(date, start = 7, end = 8)
make_date(year, month, day)
[1] "2015-03-07"

Time Zones

x <- ymd_h("2021-4-25 13", tz = "America/Los_Angeles")

x
[1] "2021-04-25 13:00:00 PDT"


force_tz()

  • same clock time as input, new time zone
force_tz(x, tzone = "America/Denver")
[1] "2021-04-25 13:00:00 MDT"

with_tz()

  • new clock time, as it would appear in a different time zone
with_tz(x, tzone = "America/Denver")
[1] "2021-04-25 14:00:00 MDT"

Time of Day

x
[1] "2021-04-25 13:00:00 PDT"


  • am()
  • pm()

TRUE or FALSE values for whether the time occurs in the AM or PM

am(x)
[1] FALSE
pm(x)
[1] TRUE